package com.ruoyi.teaching.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.teaching.vo.YaStudentScoreVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.teaching.domain.YaWeeklyReports;
import com.ruoyi.teaching.service.IYaWeeklyReportsService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 周报Controller
 * 
 * @author ZBX
 * @date 2025-03-14
 */
@RestController
@RequestMapping("/weekly_reports")
public class YaWeeklyReportsController extends BaseController
{
    @Autowired
    private IYaWeeklyReportsService yaWeeklyReportsService;

    /**
     * 查询周报列表
     */
    @RequiresPermissions("teaching:weekly_reports:list")
    @GetMapping("/list")
    public TableDataInfo list(YaWeeklyReports yaWeeklyReports)
    {
        startPage();
        List<YaWeeklyReports> list = yaWeeklyReportsService.selectYaWeeklyReportsList(yaWeeklyReports);
        return getDataTable(list);
    }

    /**
     * 导出周报列表
     */
    @RequiresPermissions("teaching:weekly_reports:export")
    @Log(title = "周报", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YaWeeklyReports yaWeeklyReports)
    {
        List<YaWeeklyReports> list = yaWeeklyReportsService.selectYaWeeklyReportsList(yaWeeklyReports);
        ExcelUtil<YaWeeklyReports> util = new ExcelUtil<YaWeeklyReports>(YaWeeklyReports.class);
        util.exportExcel(response, list, "周报数据");
    }

    /**
     * 获取周报详细信息
     */
    @RequiresPermissions("teaching:weekly_reports:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(yaWeeklyReportsService.selectYaWeeklyReportsById(id));
    }

    /**
     * 新增周报
     */
    @RequiresPermissions("teaching:weekly_reports:add")
    @Log(title = "周报", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YaWeeklyReports yaWeeklyReports)
    {
        // 插入新记录，并自动填充生成的主键到 yaWeeklyReports 的 id 属性
        yaWeeklyReportsService.insertYaWeeklyReports(yaWeeklyReports);

        // 获取生成的主键值
        Long generatedId = yaWeeklyReports.getId();

        // 返回包含生成主键的结果给前端
        return AjaxResult.success("插入成功", generatedId);
    }

    /**
     * 录入周报学生成绩
     */
    @RequiresPermissions("teaching:weekly_reports:add")
    @Log(title = "周报", businessType = BusinessType.INSERT)
    @PostMapping("/addStudentScore")
    public AjaxResult addStudentScore(@RequestBody List<YaStudentScoreVo> yaStudentScoreVo)
    {
        int rows = yaWeeklyReportsService.batchInsert(yaStudentScoreVo);
        if (rows > 0) {
            return AjaxResult.success("成绩录入成功");
        } else {
            return AjaxResult.error("成绩录入失败");
        }
    }
    /**
     * 获取周报学生成绩
     */
    @RequiresPermissions("teaching:weekly_reports:list")
    @GetMapping("/list/getStudentScore/{reportId}")
    public List<YaStudentScoreVo> getStudentScore(@PathVariable Long reportId)
    {
        List<YaStudentScoreVo> list = yaWeeklyReportsService.getStudentScore(reportId);
        return list;
    }
    /**
     * 删除周报学生成绩
     */
    @RequiresPermissions("teaching:weekly_reports:remove")
    @Log(title = "周报", businessType = BusinessType.DELETE)
    @DeleteMapping("/delStudentScore/{reportId}")
    public AjaxResult delStudentScore(@PathVariable Long reportId)
    {
        return toAjax(yaWeeklyReportsService.deleteStudentScore(reportId));
    }



    /**
     * 修改周报
     */
    @RequiresPermissions("teaching:weekly_reports:edit")
    @Log(title = "周报", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YaWeeklyReports yaWeeklyReports)
    {
        return toAjax(yaWeeklyReportsService.updateYaWeeklyReports(yaWeeklyReports));
    }

    /**
     * 删除周报
     */
    @RequiresPermissions("teaching:weekly_reports:remove")
    @Log(title = "周报", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yaWeeklyReportsService.deleteYaWeeklyReportsByIds(ids));
    }
}
